﻿// -------------------------------------------------------------------------------------------------------------------------------
// <copyright file="CMFlattenDenseMatrixExtensions.cs" company="Exul">
//     Copyright (c) Exul. All rights reserved.
// </copyright>
// <author>
//     Exul
// </author>
// -------------------------------------------------------------------------------------------------------------------------------

namespace ExulLibrary.Mathematics.LinearAlgebra.Sequential
{
    #region Usings

    using System;
    using System.Globalization;

    using ExulLibrary.Exceptions;

    #endregion

    /// <summary>
    ///     Provides extension methods for <see cref="CMFlattenDenseMatrix{T}"/>.
    /// </summary>
    public static class CMFlattenDenseMatrixExtensions
    {
        #region Methods

        #region public

        /// <summary>
        ///     Assigns the second matrix to the first matrix.
        /// </summary>
        /// <typeparam name="T">
        ///     The type of values of matrix elements.
        /// </typeparam>
        /// <param name="target">
        ///     The matrix whose elements should be assigned.
        /// </param>
        /// <param name="source">
        ///     The matrix whose elements should be copied.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="target"/> is <see langword="null"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="source"/> is <see langword="null"/>.
        ///     </para>
        /// </exception>
        /// <remarks>
        ///     The number of rows of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.RowsCount,<paramref name="source"/>.RowsCount).
        ///     The number of columns of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.ColumnsCount,<paramref name="source"/>.ColumnsCount).
        /// </remarks>
        public static void Assign<T>(this CMFlattenDenseMatrix<T> target, CMFlattenDenseMatrix<T> source)
        {
            #if SAFE

            if (target == null)
            {
                throw new ArgumentNullException("target");
            }

            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            #endif

            var sourceValues = source.Values;
            var targetValues = target.Values;

            var columnsCountToAssign = Math.Min(target.ColumnsCount, source.ColumnsCount);

            var sourceRowsCount = source.RowsCount;
            var targetRowsCount = target.RowsCount;
            if (sourceRowsCount == targetRowsCount)
            {
                Array.Copy(sourceValues, 0, targetValues, 0, columnsCountToAssign * sourceRowsCount);
                return;
            }

            var rowsCountToAssign = Math.Min(targetRowsCount, sourceRowsCount);

            var sourceIndex = 0;
            var targetIndex = 0;

            for (var columnIndex = 0; columnIndex < columnsCountToAssign; columnIndex++)
            {
                Array.Copy(sourceValues, sourceIndex, targetValues, targetIndex, rowsCountToAssign);

                sourceIndex += sourceRowsCount;
                targetIndex += targetRowsCount;
            }
        }

        /// <summary>
        ///     Assigns the second matrix to the first matrix.
        /// </summary>
        /// <param name="target">
        ///     The matrix whose elements should be assigned.
        /// </param>
        /// <param name="source">
        ///     The matrix whose elements should be copied.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="target"/> is <see langword="null"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="source"/> is <see langword="null"/>.
        ///     </para>
        /// </exception>
        /// <remarks>
        ///     The number of rows of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.RowsCount,<paramref name="source"/>.RowsCount).
        ///     The number of columns of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.ColumnsCount,<paramref name="source"/>.ColumnsCount).
        /// </remarks>
        public static void Assign(this CMFlattenDenseMatrix<bool> target, CMFlattenDenseMatrix<bool> source)
        {
            Assign(target, source, sizeof(bool));
        }

        /// <summary>
        ///     Assigns the second matrix to the first matrix.
        /// </summary>
        /// <param name="target">
        ///     The matrix whose elements should be assigned.
        /// </param>
        /// <param name="source">
        ///     The matrix whose elements should be copied.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="target"/> is <see langword="null"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="source"/> is <see langword="null"/>.
        ///     </para>
        /// </exception>
        /// <remarks>
        ///     The number of rows of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.RowsCount,<paramref name="source"/>.RowsCount).
        ///     The number of columns of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.ColumnsCount,<paramref name="source"/>.ColumnsCount).
        /// </remarks>
        public static void Assign(this CMFlattenDenseMatrix<byte> target, CMFlattenDenseMatrix<byte> source)
        {
            Assign(target, source, sizeof(byte));
        }

        /// <summary>
        ///     Assigns the second matrix to the first matrix.
        /// </summary>
        /// <param name="target">
        ///     The matrix whose elements should be assigned.
        /// </param>
        /// <param name="source">
        ///     The matrix whose elements should be copied.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="target"/> is <see langword="null"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="source"/> is <see langword="null"/>.
        ///     </para>
        /// </exception>
        /// <remarks>
        ///     The number of rows of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.RowsCount,<paramref name="source"/>.RowsCount).
        ///     The number of columns of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.ColumnsCount,<paramref name="source"/>.ColumnsCount).
        /// </remarks>
        [CLSCompliant(false)]
        public static void Assign(this CMFlattenDenseMatrix<sbyte> target, CMFlattenDenseMatrix<sbyte> source)
        {
            Assign(target, source, sizeof(sbyte));
        }

        /// <summary>
        ///     Assigns the second matrix to the first matrix.
        /// </summary>
        /// <param name="target">
        ///     The matrix whose elements should be assigned.
        /// </param>
        /// <param name="source">
        ///     The matrix whose elements should be copied.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="target"/> is <see langword="null"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="source"/> is <see langword="null"/>.
        ///     </para>
        /// </exception>
        /// <remarks>
        ///     The number of rows of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.RowsCount,<paramref name="source"/>.RowsCount).
        ///     The number of columns of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.ColumnsCount,<paramref name="source"/>.ColumnsCount).
        /// </remarks>
        public static void Assign(this CMFlattenDenseMatrix<char> target, CMFlattenDenseMatrix<char> source)
        {
            Assign(target, source, sizeof(char));
        }

        /// <summary>
        ///     Assigns the second matrix to the first matrix.
        /// </summary>
        /// <param name="target">
        ///     The matrix whose elements should be assigned.
        /// </param>
        /// <param name="source">
        ///     The matrix whose elements should be copied.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="target"/> is <see langword="null"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="source"/> is <see langword="null"/>.
        ///     </para>
        /// </exception>
        /// <remarks>
        ///     The number of rows of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.RowsCount,<paramref name="source"/>.RowsCount).
        ///     The number of columns of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.ColumnsCount,<paramref name="source"/>.ColumnsCount).
        /// </remarks>
        public static void Assign(this CMFlattenDenseMatrix<float> target, CMFlattenDenseMatrix<float> source)
        {
            Assign(target, source, sizeof(float));
        }

        /// <summary>
        ///     Assigns the second matrix to the first matrix.
        /// </summary>
        /// <param name="target">
        ///     The matrix whose elements should be assigned.
        /// </param>
        /// <param name="source">
        ///     The matrix whose elements should be copied.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="target"/> is <see langword="null"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="source"/> is <see langword="null"/>.
        ///     </para>
        /// </exception>
        /// <remarks>
        ///     The number of rows of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.RowsCount,<paramref name="source"/>.RowsCount).
        ///     The number of columns of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.ColumnsCount,<paramref name="source"/>.ColumnsCount).
        /// </remarks>
        public static void Assign(this CMFlattenDenseMatrix<double> target, CMFlattenDenseMatrix<double> source)
        {
            Assign(target, source, sizeof(double));
        }

        /// <summary>
        ///     Assigns the second matrix to the first matrix.
        /// </summary>
        /// <param name="target">
        ///     The matrix whose elements should be assigned.
        /// </param>
        /// <param name="source">
        ///     The matrix whose elements should be copied.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="target"/> is <see langword="null"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="source"/> is <see langword="null"/>.
        ///     </para>
        /// </exception>
        /// <remarks>
        ///     The number of rows of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.RowsCount,<paramref name="source"/>.RowsCount).
        ///     The number of columns of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.ColumnsCount,<paramref name="source"/>.ColumnsCount).
        /// </remarks>
        public static void Assign(this CMFlattenDenseMatrix<short> target, CMFlattenDenseMatrix<short> source)
        {
            Assign(target, source, sizeof(short));
        }

        /// <summary>
        ///     Assigns the second matrix to the first matrix.
        /// </summary>
        /// <param name="target">
        ///     The matrix whose elements should be assigned.
        /// </param>
        /// <param name="source">
        ///     The matrix whose elements should be copied.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="target"/> is <see langword="null"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="source"/> is <see langword="null"/>.
        ///     </para>
        /// </exception>
        /// <remarks>
        ///     The number of rows of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.RowsCount,<paramref name="source"/>.RowsCount).
        ///     The number of columns of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.ColumnsCount,<paramref name="source"/>.ColumnsCount).
        /// </remarks>
        [CLSCompliant(false)]
        public static void Assign(this CMFlattenDenseMatrix<ushort> target, CMFlattenDenseMatrix<ushort> source)
        {
            Assign(target, source, sizeof(ushort));
        }

        /// <summary>
        ///     Assigns the second matrix to the first matrix.
        /// </summary>
        /// <param name="target">
        ///     The matrix whose elements should be assigned.
        /// </param>
        /// <param name="source">
        ///     The matrix whose elements should be copied.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="target"/> is <see langword="null"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="source"/> is <see langword="null"/>.
        ///     </para>
        /// </exception>
        /// <remarks>
        ///     The number of rows of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.RowsCount,<paramref name="source"/>.RowsCount).
        ///     The number of columns of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.ColumnsCount,<paramref name="source"/>.ColumnsCount).
        /// </remarks>
        public static void Assign(this CMFlattenDenseMatrix<int> target, CMFlattenDenseMatrix<int> source)
        {
            Assign(target, source, sizeof(int));
        }

        /// <summary>
        ///     Assigns the second matrix to the first matrix.
        /// </summary>
        /// <param name="target">
        ///     The matrix whose elements should be assigned.
        /// </param>
        /// <param name="source">
        ///     The matrix whose elements should be copied.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="target"/> is <see langword="null"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="source"/> is <see langword="null"/>.
        ///     </para>
        /// </exception>
        /// <remarks>
        ///     The number of rows of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.RowsCount,<paramref name="source"/>.RowsCount).
        ///     The number of columns of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.ColumnsCount,<paramref name="source"/>.ColumnsCount).
        /// </remarks>
        [CLSCompliant(false)]
        public static void Assign(this CMFlattenDenseMatrix<uint> target, CMFlattenDenseMatrix<uint> source)
        {
            Assign(target, source, sizeof(uint));
        }

        /// <summary>
        ///     Assigns the second matrix to the first matrix.
        /// </summary>
        /// <param name="target">
        ///     The matrix whose elements should be assigned.
        /// </param>
        /// <param name="source">
        ///     The matrix whose elements should be copied.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="target"/> is <see langword="null"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="source"/> is <see langword="null"/>.
        ///     </para>
        /// </exception>
        /// <remarks>
        ///     The number of rows of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.RowsCount,<paramref name="source"/>.RowsCount).
        ///     The number of columns of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.ColumnsCount,<paramref name="source"/>.ColumnsCount).
        /// </remarks>
        public static void Assign(this CMFlattenDenseMatrix<long> target, CMFlattenDenseMatrix<long> source)
        {
            Assign(target, source, sizeof(long));
        }

        /// <summary>
        ///     Assigns the second matrix to the first matrix.
        /// </summary>
        /// <param name="target">
        ///     The matrix whose elements should be assigned.
        /// </param>
        /// <param name="source">
        ///     The matrix whose elements should be copied.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="target"/> is <see langword="null"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="source"/> is <see langword="null"/>.
        ///     </para>
        /// </exception>
        /// <remarks>
        ///     The number of rows of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.RowsCount,<paramref name="source"/>.RowsCount).
        ///     The number of columns of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.ColumnsCount,<paramref name="source"/>.ColumnsCount).
        /// </remarks>
        [CLSCompliant(false)]
        public static void Assign(this CMFlattenDenseMatrix<ulong> target, CMFlattenDenseMatrix<ulong> source)
        {
            Assign(target, source, sizeof(ulong));
        }

        /// <summary>
        ///     Assigns the second matrix to the specified elements of the first matrix.
        /// </summary>
        /// <typeparam name="T">
        ///     The type of values of matrix elements.
        /// </typeparam>
        /// <param name="target">
        ///     The matrix whose elements should be assigned.
        /// </param>
        /// <param name="targetRowsIndexes">
        ///     The collection of zero-based indexes of rows of target elements.
        /// </param>
        /// <param name="targetColumnsIndexes">
        ///     The collection of zero-based indexes of columns of target elements.
        /// </param>
        /// <param name="source">
        ///     The matrix whose elements should be copied.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="target"/> is <see langword="null"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="source"/> is <see langword="null"/>.
        ///     </para>
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="targetRowsIndexes"/> is <see langword="null"/>.
        ///     </para>
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="targetColumnsIndexes"/> is <see langword="null"/>.
        ///     </para>
        /// </exception>
        /// <exception cref="ElementIndexIsOutOfRangeException">
        ///     An element of <paramref name="targetRowsIndexes"/> is negative or greater than or equal to
        ///     rows count of <paramref name="target"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         An element of <paramref name="targetColumnsIndexes"/> is negative or greater than or equal to
        ///         columns count of <paramref name="target"/>.
        ///     </para>
        /// </exception>
        /// <remarks>
        ///     The number of rows of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="source"/>.RowsCount, <paramref name="targetRowsIndexes"/>.Length).
        ///     The number of columns of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="source"/>.ColumnsCount, <paramref name="targetColumnsIndexes"/>.Length).
        /// </remarks>
        public static void Assign<T>(
            this CMFlattenDenseMatrix<T> target,
            int[] targetRowsIndexes,
            int[] targetColumnsIndexes,
            CMFlattenDenseMatrix<T> source)
        {
            #if SAFE

            if (target == null)
            {
                throw new ArgumentNullException("target");
            }

            if (targetRowsIndexes == null)
            {
                throw new ArgumentNullException("targetRowsIndexes");
            }

            if (targetColumnsIndexes == null)
            {
                throw new ArgumentNullException("targetColumnsIndexes");
            }

            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            #endif

            var targetRowsCount = target.RowsCount;
            var targetColumnsCount = target.ColumnsCount;

            var targetValues = target.Values;
            var sourceValues = source.Values;

            var sourceRowsCount = source.RowsCount;

            var rowsCountToAssign = Math.Min(targetRowsIndexes.Length, source.RowsCount);

            var columnsCountToAssign = Math.Min(targetColumnsIndexes.Length, source.ColumnsCount);
            for (var columnIndex = 0; columnIndex < columnsCountToAssign; columnIndex++)
            {
                var columnElementIndex = targetColumnsIndexes[columnIndex];

                #if SAFE

                if ((columnElementIndex < 0) || (targetColumnsCount <= columnElementIndex))
                {
                    var argumentName = string.Format(CultureInfo.CurrentCulture, "targetColumnsIndexes[{0}]", columnIndex);
                    throw new ElementIndexIsOutOfRangeException(argumentName, columnElementIndex, 0, targetColumnsCount - 1);
                }

                #endif

                var targetIndex = columnElementIndex * targetRowsCount;
                var sourceIndex = columnIndex * sourceRowsCount;

                for (var rowIndex = 0; rowIndex < rowsCountToAssign; rowIndex++)
                {
                    var rowElementIndex = targetRowsIndexes[rowIndex];

                    #if SAFE

                    if ((rowElementIndex < 0) || (targetRowsCount <= rowElementIndex))
                    {
                        var argumentName = string.Format(CultureInfo.CurrentCulture, "targetRowsIndexes[{0}]", rowIndex);
                        throw new ElementIndexIsOutOfRangeException(argumentName, rowElementIndex, 0, targetRowsCount - 1);
                    }

                    #endif

                    targetValues[targetIndex + rowElementIndex] = sourceValues[sourceIndex];

                    sourceIndex++;
                }
            }
        }

        /// <summary>
        ///     Sets the default value to all elements of the matrix.
        /// </summary>
        /// <typeparam name="T">
        ///     The type of values of matrix elements.
        /// </typeparam>
        /// <param name="matrix">
        ///     The matrix that contains elements to clear.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="matrix"/> is <see langword="null"/>.
        /// </exception>
        public static void Clear<T>(this CMFlattenDenseMatrix<T> matrix)
        {
            #if SAFE

            if (matrix == null)
            {
                throw new ArgumentNullException("matrix");
            }

            #endif

            Array.Clear(matrix.Values, 0, matrix.RowsCount * matrix.ColumnsCount);
        }

        /// <summary>
        ///     Sets the default value to all elements in the specified column of the matrix.
        /// </summary>
        /// <typeparam name="T">
        ///     The type of values of matrix elements.
        /// </typeparam>
        /// <param name="matrix">
        ///     The matrix that contains the column to clear.
        /// </param>
        /// <param name="columnIndex">
        ///     The zero-based index of the column to clear.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="matrix"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ElementIndexIsOutOfRangeException">
        ///     <paramref name="columnIndex"/> is negative or greater than or equal to columns count.
        /// </exception>
        public static void ClearColumn<T>(this CMFlattenDenseMatrix<T> matrix, int columnIndex)
        {
            #if SAFE

            if (matrix == null)
            {
                throw new ArgumentNullException("matrix");
            }

            if ((columnIndex < 0) || (matrix.ColumnsCount <= columnIndex))
            {
                throw new ElementIndexIsOutOfRangeException("columnIndex", columnIndex, 0, matrix.ColumnsCount - 1);
            }

            #endif

            Array.Clear(matrix.Values, columnIndex * matrix.RowsCount, matrix.RowsCount);
        }

        /// <summary>
        ///     Sets the default value to all elements in the specified row of the matrix.
        /// </summary>
        /// <typeparam name="T">
        ///     The type of values of matrix elements.
        /// </typeparam>
        /// <param name="matrix">
        ///     The matrix that contains the row to clear.
        /// </param>
        /// <param name="rowIndex">
        ///     The zero-based index of the row to clear.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="matrix"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ElementIndexIsOutOfRangeException">
        ///     <paramref name="rowIndex"/> is negative or greater than or equal to rows count.
        /// </exception>
        public static void ClearRow<T>(this CMFlattenDenseMatrix<T> matrix, int rowIndex)
        {
            #if SAFE

            if (matrix == null)
            {
                throw new ArgumentNullException("matrix");
            }

            if ((rowIndex < 0) || (matrix.RowsCount <= rowIndex))
            {
                throw new ElementIndexIsOutOfRangeException("rowIndex", rowIndex, 0, matrix.RowsCount - 1);
            }

            #endif

            var rowsCount = matrix.RowsCount;
            var values = matrix.Values;

            var elementsCount = rowsCount * matrix.ColumnsCount;
            for (var elementIndex = rowIndex; elementIndex < elementsCount; elementIndex += rowsCount)
            {
                values[elementIndex] = default(T);
            }
        }

        /// <summary>
        ///     Creates a deep copy of the matrix.
        /// </summary>
        /// <typeparam name="T">
        ///     The type of values of matrix elements.
        /// </typeparam>
        /// <param name="source">
        ///     The source matrix.
        /// </param>
        /// <returns>
        ///     A deep copy of the matrix.
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> is <see langword="null"/>.</exception>
        public static CMFlattenDenseMatrix<T> Clone<T>(this CMFlattenDenseMatrix<T> source)
        {
            #if SAFE

            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            #endif

            var result = new CMFlattenDenseMatrix<T>(source.RowsCount, source.ColumnsCount);
            result.Assign(source);
            return result;
        }

        /// <summary>
        ///     Gets the element in the matrix at the specified location.
        /// </summary>
        /// <typeparam name="T">
        ///     The type of values of matrix elements.
        /// </typeparam>
        /// <param name="matrix">
        ///     The matrix that contains the element to get.
        /// </param>
        /// <param name="rowIndex">
        ///     The zero-based index of row of the element to get.
        /// </param>
        /// <param name="columnIndex">
        ///     The zero-based index of column of the element to get.
        /// </param>
        /// <returns>
        ///     The element in the matrix at the specified location.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="matrix"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ElementIndexIsOutOfRangeException">
        ///     <paramref name="rowIndex"/> is negative or greater than or equal to
        ///     the number of rows of <paramref name="matrix"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="columnIndex"/> is negative or greater than or equal to
        ///         the number of columns of <paramref name="matrix"/>.
        ///     </para>
        /// </exception>
        public static T GetAt<T>(this CMFlattenDenseMatrix<T> matrix, int rowIndex, int columnIndex)
        {
            #if SAFE

            if (matrix == null)
            {
                throw new ArgumentNullException("matrix");
            }

            if ((rowIndex < 0) || (matrix.RowsCount <= rowIndex))
            {
                throw new ElementIndexIsOutOfRangeException("rowIndex", rowIndex, 0, matrix.RowsCount - 1);
            }

            if ((columnIndex < 0) || (matrix.ColumnsCount <= columnIndex))
            {
                throw new ElementIndexIsOutOfRangeException("columnIndex", columnIndex, 0, matrix.ColumnsCount - 1);
            }

            #endif

            return matrix.Values[(columnIndex * matrix.RowsCount) + rowIndex];
        }

        /// <summary>
        ///     Sets the element in the matrix at the specified location.
        /// </summary>
        /// <typeparam name="T">
        ///     The type of values of matrix elements.
        /// </typeparam>
        /// <param name="matrix">
        ///     The matrix that contains the element to set.
        /// </param>
        /// <param name="newValue">
        ///     The new value for the element.
        /// </param>
        /// <param name="rowIndex">
        ///     The zero-based index of row of the element to set.
        /// </param>
        /// <param name="columnIndex">
        ///     The zero-based index of column of the element to set.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="matrix"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ElementIndexIsOutOfRangeException">
        ///     <paramref name="rowIndex"/> is negative or greater than or equal to
        ///     the number of rows of <paramref name="matrix"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="columnIndex"/> is negative or greater than or equal to
        ///         the number of columns of <paramref name="matrix"/>.
        ///     </para>
        /// </exception>
        public static void SetAt<T>(this CMFlattenDenseMatrix<T> matrix, T newValue, int rowIndex, int columnIndex)
        {
            #if SAFE

            if (matrix == null)
            {
                throw new ArgumentNullException("matrix");
            }

            if ((rowIndex < 0) || (matrix.RowsCount <= rowIndex))
            {
                throw new ElementIndexIsOutOfRangeException("rowIndex", rowIndex, 0, matrix.RowsCount - 1);
            }

            if ((columnIndex < 0) || (matrix.ColumnsCount <= columnIndex))
            {
                throw new ElementIndexIsOutOfRangeException("columnIndex", columnIndex, 0, matrix.ColumnsCount - 1);
            }

            #endif

            matrix.Values[(columnIndex * matrix.RowsCount) + rowIndex] = newValue;
        }

        #endregion

        #region private

        /// <summary>
        ///     Assigns the second matrix to the first matrix.
        /// </summary>
        /// <typeparam name="T">
        ///     The type of values of matrix elements.
        /// </typeparam>
        /// <param name="target">
        ///     The matrix whose elements should be assigned.
        /// </param>
        /// <param name="source">
        ///     The matrix whose elements should be copied.
        /// </param>
        /// <param name="itemSize">
        ///     The size of one element of matrix in bytes.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="target"/> is <see langword="null"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="source"/> is <see langword="null"/>.
        ///     </para>
        /// </exception>
        /// <remarks>
        ///     The number of rows of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.RowsCount,<paramref name="source"/>.RowsCount).
        ///     The number of columns of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.ColumnsCount,<paramref name="source"/>.ColumnsCount).
        /// </remarks>
        private static void Assign<T>(CMFlattenDenseMatrix<T> target, CMFlattenDenseMatrix<T> source, int itemSize)
        {
            #if SAFE

            if (target == null)
            {
                throw new ArgumentNullException("target");
            }

            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            #endif

            var sourceValues = source.Values;
            var targetValues = target.Values;

            var columnsCount = Math.Min(target.ColumnsCount, source.ColumnsCount);

            var sourceColumnLength = source.RowsCount * itemSize;
            var targetColumnLength = target.RowsCount * itemSize;
            if (sourceColumnLength == targetColumnLength)
            {
                Buffer.BlockCopy(sourceValues, 0, targetValues, 0, columnsCount * sourceColumnLength);
                return;
            }

            var columnLength = Math.Min(targetColumnLength, sourceColumnLength);

            var sourceOffset = 0;
            var targetOffset = 0;

            for (var columnIndex = 0; columnIndex < columnsCount; columnIndex++)
            {
                Buffer.BlockCopy(sourceValues, sourceOffset, targetValues, targetOffset, columnLength);

                sourceOffset += sourceColumnLength;
                targetOffset += targetColumnLength;
            }
        }

        #endregion

        #endregion
    }
}